<div id="phpShell">
	<div style='width:550px' class="text ui-widget-content ui-corner-all p10">
		<div class="ui-state-default ui-corner-top p10">
			Результат
			<div style='float:right;' class='f_s_12'>
				<div id="cleareResult">
					Очитить консоль
				</div>
			</div>
		</div>
		<div id="result" style='width:538px;max-height:275px;min-height:100px;overflow-x:hidden;overflow-y:scroll;margin-top: -2px;' class="text ui-widget-content ui-corner-bottom p5"></div>
	</div>
	<div style='width:550px' class="text ui-widget-content ui-corner-top p10">
		<div class="ui-state-default ui-corner-top p10">
			PHP Консоль
			<div style='float:right;' class='f_s_12 nav'>
				<a class='but' data-action='function(){$("#shell").trigger("keyup",[true]);}' data-opt='{"icon":"power"}'>Пуск</a>
				<a class='but' data-opt='{"action":"jAlert","content":"div.help","icon":"help","text":false}'>Помощь</a>
				<div class="help hide_">
					<div class="t_i_10 ui-widget-content ui-corner-all p5">
						<div class='ui-corner-all ui-widget-content p5'>
							Для запуска кода необходимо нажать
							<b>Ctrl+Enter</b>
						</div>
					</div>
					<div class="t_i_10 ui-widget-content ui-corner-all p5">
						<div class='ui-state-default ui-corner-top b p5'>История команд</div>
						<div class='ui-corner-bottom ui-widget-content p5'>
							Так же есть история выполненных команд.<br>
							Для передвижения по истори необходимо нажать
							<b>Ctrl+Up или Ctrl+Down</b>
						</div>
					</div>
					<div class="t_i_10 ui-widget-content ui-corner-all p5">
						<div class='ui-state-default ui-corner-top b p5'>Автозамена</div>
						<div class='ui-widget-content autoReplaceHelp p5'>
							Автозамена работает для группы последних символов,которые находятся в конце ткстового поля.
						</div>
						<div class='ui-corner-bottom ui-widget-content autoReplaceHelp p5'>
							<table border="1" cellspacing="0" cellpadding="2" style="width:100%">
								<caption>Список автозамены</caption>
								<tr><th>Из</th><th>&#8594;</th><th>Результат</th></tr>
							</table>
						</div>
					</div>
				</div>
			</div>
		</div>
		<textarea id="shell" style='width:538px;height:130px;margin-top: -2px;' class="text ui-widget-content ui-corner-bottom p5"></textarea>
	</div>
</div>
<script type="text/javascript" charset="utf-8">
	//********************************************************************************
	(function(){
		var res 	= $('#result');
		var shell 	= $("#shell");
		var commandStack 		= [];
		var commandStackIndex 	= -1;
		$('#cleareResult').but({
			icon:"close"
		}).click( function() {
			res.html('');
			return false;
		});
		var command = shell.val();
		var buffer 	= "";
		var replace_list = {"po":"$_POST","ec":"echo ","pri":"print_r($","ses":"$_SESSION","get":"$_GET","ser":"$_SERVER","fgc":"file_get_contents("};
		shell.data('replace_list',replace_list);
		refreshAutoComplite();
		
		function refreshAutoComplite(data){
				if(data&&typeof(data)=="object")shell.data('replace_list',data);
				var autoReplace_list = shell.data('replace_list');
			    var help = $('#phpShell .autoReplaceHelp table');
				for(var i in autoReplace_list){
					help.append("<tr><td>"+i+"</td><td>&#8594;</td><td>"+autoReplace_list[i]+"</td></tr>");
				}
			
		}
		var commandLength=0;
		$(shell).keyup( function(e,button) {
				var char_ = command;
					command = shell.val();
			if(command.length>char_.length){
				char_ = command.replace(/(?:.|\n|\r|\f)+(.)$/g,'$1');
				if(char_!=""&&char_!="\n"&&!/\s/.test(char_)&&!/\n/.test(char_)&&char_!="("&&char_!=")"){
					buffer+=char_;
				}else if(/\s/.test(char_)||char_=="("||char_==")"){
					buffer="";
				}
				var autoReplace_list = shell.data('replace_list')//{"po":"$_POST","ec":"echo ","pri":"print_r($","ses":"$_SESSION","get":"$_GET","ser":"$_SERVER"};
				for(var i in autoReplace_list){
					if(buffer==i){
						shell.val(command.replace((new RegExp(i+"$")),autoReplace_list[i]));
						buffer="";
					}
				}
			}else{
				buffer="";
			}
			if(e.keyCode==8||e.keyCode==46)buffer="";
		 	//console.log("buf - "+buffer+" keyCode-"+e.keyCode);
			
			if((e.keyCode == 13 && e.ctrlKey)||button) {
				if(command=="")return false;
				$.post('index/shell', {
					eval:command
				}, function(e) {
					commandStack.push(command);
					commandStackIndex++;
					commandLength++;					
					var str = "<div class='m5 ui-widget-content ui-corner-all'><div class='p5 ui-widget-content ui-corner-top'><span style='margin-left:-6px;margin-right:5px' class='b p5 ui-state-default ui-corner-left'>№"+commandLength+"</span>"+command+"</div><div class='p5 ui-widget-content ui-corner-bottom'><textarea style='width:98%;min-height:200px;resize: none;'>"+e+"</textarea></div></div>";
					res.prepend(str);
				});
			} else if(e.keyCode == 38 && e.ctrlKey) {
				commandStackIndex = commandStackIndex<commandStack.length-1? commandStackIndex+1 : commandStackIndex;
				shell.val(commandStack[commandStackIndex]);
				highLight();
			} else if(e.keyCode == 40 && e.ctrlKey) {
				commandStackIndex = commandStackIndex>0? commandStackIndex-1 : 0;
				shell.val(commandStack[commandStackIndex]);
				highLight();
			} else if(e.keyCode == 9) {
	
				return false;
			}
		});
		
		function highLight() {
			if(commandStackIndex<commandStack.length-1) {
				if(!shell.hasClass('ui-state-highlight'))
					shell.addClass('ui-state-highlight');
				if(commandStackIndex==0&&!shell.hasClass('ui-state-error')) {
					if(shell.hasClass('ui-state-highlight'))
						shell.removeClass('ui-state-highlight');
					shell.addClass('ui-state-error');
				} else if(commandStackIndex!=0&&shell.hasClass('ui-state-error')) {
					if(!shell.hasClass('ui-state-highlight'))
						shell.addClass('ui-state-highlight');
					shell.removeClass('ui-state-error');
				}
			} else {
				if(shell.hasClass('ui-state-highlight'))
					shell.removeClass('ui-state-highlight');
			}
		}
	})();
	//********************************************************************************
</script>